package live;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;


/* This class firstly adds a right-click menu to all components copied from the original 
 * code, and add mouse listeners so we can inspect them. On click to these components
 * the information about them is captured and mapped to the corresponding object in 
 * Aspect.aj hashMap, captured from the pointcuts
 */
public class insertInspectInfo extends JPopupMenu {

	public JMenuItem anItem;


	
	public insertInspectInfo() {
		anItem = new JMenuItem("Inspect Element");
		anItem.addActionListener(new ActionListener()
		{
		     public void actionPerformed(ActionEvent e)
		     {
		    	 PopClickListener pcl = new PopClickListener();
		    	 pcl.inspectInfoCaptured(PopClickListener.cmpCaptured);
		     }
		});
		add(anItem);
	}
}

class PopClickListener extends MouseAdapter implements MouseListener {
	public JPopupMenu popup;
	public JMenuItem anItem;
	public JComponent comp;
	public LineBorder borderColor;
	
	public static JComponent cmpCaptured;
	public static String getName;
	public static String getSourceLocation;

	public void mousePressed(MouseEvent e) {		
		if (e.isPopupTrigger())
			doPop(e);
	}

	public void mouseReleased(MouseEvent e) {
		if (e.isPopupTrigger())
			doPop(e);
	}

	public void mouseEntered(MouseEvent e) {
	}
	public void mouseClicked(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	public void WritableHyperlink(int col, int row, java.io.File file,
			java.lang.String desc) {

	}
	/* Main function which gets the JComponent clicked on the UI and maps to the 
	 * corresponding object in the Aspect.capturedData hashMap
	 * For mapping, the key in the hashMap is the hashCode of the components captured
	 * thus it is unique and easier to do matching
	 * It calls the infoCaptured() function to insert these information in a JTable
	 */
	private void doPop(MouseEvent e) {
		
		cmpCaptured = (JComponent) e.getComponent();
		
		insertInspectInfo menu = new insertInspectInfo();
		menu.show(e.getComponent(), e.getX(), e.getY());
		InspectElement.model.setColumnCount(0);
	}

	/* This function inserts the information of the inspected components such as:
	 * Label Name, Source code line, position, background color and so on
	 * and inserts them in a JTable
	 */
	public static void insertInfoTable(JComponent cmp, int hashCode) {

		String isValid = null;
		String isEnabled = null;
		if (cmp.isValid()) {
			isValid = "YES";
		} else {
			isValid = "NO";
		}
		if (cmp.isEnabled()) {
			isEnabled = "YES";
		} else {
			isEnabled = "NO";
		}
		if(Aspect.capturedShapes.get(hashCode).get(0) == null){
			getName = "information not available";
		}
		else
		{
			getName = Aspect.capturedShapes.get(hashCode).get(0).toString();
		}
		
		if(Aspect.capturedShapes.get(hashCode).get(1) == null){
			getSourceLocation = "information not available";
		}
		else{
			getSourceLocation = Aspect.capturedShapes.get(hashCode).get(1);
		}
		
		InspectElement.model.addColumn("Name", new String[] { "Component Label",
				"Parent Component", "Source Code Line", "Class Name", "Size",
				"Component Position", "Location", "Location on Screen",
				"Root Pane", "Background Color", "Foreground Color", "isValid",
				"isEnabled" });
		InspectElement.model.addColumn(
				"Value",
				new String[] {
						
						getName,
						cmp.getParent().getClass().getCanonicalName(),
						"<html><font color=blue>" + getSourceLocation +"</font></html>",
						cmp.getClass().toString(),
						cmp.getSize().toString(),
						cmp.getAlignmentX() + "," + cmp.getAlignmentY(),
						cmp.getX() + "," + cmp.getY(),
						cmp.getLocationOnScreen().x + ","
								+ cmp.getLocationOnScreen().y,
						cmp.getRootPane().toString(),
						cmp.getBackground().toString(),
						cmp.getForeground().toString(), isValid, isEnabled });
	}
	


	public void inspectInfoCaptured(JComponent cmp){


		int hashCode = cmp.hashCode();
		if (Aspect.capturedData.containsKey(hashCode)) {
			if (Aspect.capturedData.get(hashCode) != null) {
				insertInfoTable(cmp, hashCode);
			}
		}
	}
}
